package com.wx.activitys.valsday.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.github.sd4324530.fastweixin.api.JsAPI;
import com.github.sd4324530.fastweixin.api.response.GetSignatureResponse;
import com.github.sd4324530.fastweixin.api.response.GetUserInfoResponse;
import com.wx.activitys.base.controller.BaseController;
import com.wx.activitys.base.utils.Common;
import com.wx.activitys.valsday.model.ValsDayAccess;
import com.wx.activitys.valsday.service.ValsDayAccessService;


@Controller
@RequestMapping(value = "/valsday")
public class ValsDayController extends BaseController{
	
	public static final String ROOT_NAME="valsday";
	public static final String BASE_PATH="http://wxh5.xuanzhen-tech.com";
	//public static final String BASE_PATH="http://localhost:8080/wx_activity";
	public static final String BASE_URL=BASE_PATH+"/"+ROOT_NAME;
	@Autowired
	ValsDayAccessService valsDayAccessService;
	@Autowired
	private JsAPI jsAPI;
	
	
	@RequestMapping(value="/play.html",method = {RequestMethod.GET,RequestMethod.POST})
	public String play(Model model, HttpServletRequest request){
		String currentPage=BASE_URL+"/play.html";
		GetSignatureResponse signObj=jsAPI.getSignature(currentPage);
		model.addAttribute("basePath", BASE_PATH);
		model.addAttribute("signObj", signObj);
		model.addAttribute("baseUrl", BASE_URL);
		return "valsday/form";
	}
	
	@RequestMapping(value="/form.html",method=RequestMethod.POST)
	public ModelAndView form(ModelAndView model,ValsDayAccess valsDayAccess, HttpServletRequest request,HttpSession session){
		System.out.println("姓名："+valsDayAccess.getName()+"\t头像："+valsDayAccess.getImg()+"\t openid："+valsDayAccess.getOpenid());
		//valsDayAccess.setOpenid("dsadkhduqeqeuirhq");
		//上线后使用
		String openid = ((GetUserInfoResponse)session.getAttribute("userInfo")).getOpenid();
		if (openid!=null &&!"".equals(openid)) {
			valsDayAccess.setOpenid(openid);
			List<ValsDayAccess> list = valsDayAccessService.selectValsDayAccessByOpenid(valsDayAccess);
			//验证是否已存在
			if(list.size()>0){
				//已存在时 修改状态为0
				valsDayAccessService.updateValsDayAccessStatus(list.get(0));
				System.out.println("已存在，修改成功");
			}
			//建立插入的对象 
			Date date = new Date();
			valsDayAccess.setCreateTime(date);
			valsDayAccess.setStatus(1);
			valsDayAccess.setImg("valsday/"+valsDayAccess.getImg());
			int random1 = (int)Math.floor(Math.random()*20+10);
			int random2 = (int)Math.floor(Math.random()*20+10);
			int random3 = (int)Math.floor(Math.random()*30+30);
			int random4 = (int)Math.floor(Math.random()*30+30);
			int random5 = (int)Math.floor(Math.random()*10+60);
			int random6 = (int)Math.floor(Math.random()*10+70);
			int random7 = (int)Math.floor(Math.random()*20+80);
			valsDayAccess.setRandomOne(random1);
			valsDayAccess.setRandomTwo(random2);
			valsDayAccess.setRandomThree(random3);
			valsDayAccess.setRandomFour(random4);
			valsDayAccess.setRandomFive(random5);
			valsDayAccess.setRandomSix(random6);
			valsDayAccess.setRandomSeven(random7);
			
			//插入
			int count = valsDayAccessService.insertValsDayAccess(valsDayAccess);
			System.out.println("成功插入"+count+"条");
		}
		model.setViewName("redirect:show.html");
		return model;
	}
	
	//end页控制器
	@RequestMapping(value="/show.html", method={RequestMethod.GET})
	public String show(Model model, HttpServletRequest request,HttpSession session){
		ValsDayAccess valsDayAccess = new ValsDayAccess();
		//上线后使用
		String openid = ((GetUserInfoResponse)session.getAttribute("userInfo")).getOpenid();
		if(openid==null ||"".equals(openid)){
			return "error/error-500";
		}
		valsDayAccess.setOpenid(openid);
//		valsDayAccess.setOpenid("dsadkhduqeqeuirhq");
		List<ValsDayAccess> list = valsDayAccessService.selectValsDayAccessByOpenid(valsDayAccess);
		if (list.size()>0) {
			model.addAttribute("vals",list.get(0));
			model.addAttribute("imgPath",Common.IMG_GET_BASEPATH);
		}else{
			return "error/error-500";
		}
		String currentPage=BASE_URL+"/show.html";
		GetSignatureResponse signObj=jsAPI.getSignature(currentPage);
		model.addAttribute("signObj", signObj);
		model.addAttribute("basePath", BASE_PATH);
		model.addAttribute("baseUrl", BASE_URL);
		
		return "valsday/show";
	}
	
	//分享出去页的控制器
	@RequestMapping(value="/share.html",method={RequestMethod.GET})
	public String share(Model model, HttpServletRequest request,String key){
		ValsDayAccess valsDayAccess = new ValsDayAccess();
		valsDayAccess.setOpenid(key);
		List<ValsDayAccess> list = valsDayAccessService.selectValsDayAccessByOpenid(valsDayAccess);
		if (list.size()>0) {
			model.addAttribute("vals",list.get(0));
		}else{
			return "error/error-500";
		}
		model.addAttribute("imgPath",Common.IMG_GET_BASEPATH);
		StringBuilder currentPage=new StringBuilder(BASE_URL+"/share.html?key="+key);
		String from = request.getParameter("from");
		if (from!=null) {
			currentPage.append("&from="+from);
		}
		String isappinstalled = request.getParameter("isappinstalled");
		if (isappinstalled!=null) {
			currentPage.append("&isappinstalled="+isappinstalled);
		}
		GetSignatureResponse signObj=jsAPI.getSignature(currentPage.toString());
		model.addAttribute("signObj", signObj);
		model.addAttribute("basePath", BASE_PATH);
		model.addAttribute("baseUrl", BASE_URL);
		
		return "valsday/share";
	}
	
}
